package com.yaolong.api.modules.opms.controller;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yaolong.api.modules.opms.entity.FixResident;
import com.yaolong.api.modules.opms.service.FixResidentService;
import com.yaolong.api.modules.opms.vo.DictVo;
import com.yaolong.api.modules.utils.ExcelUtil2;
import com.yaolong.commons.exceptions.BusinessException;
import com.yaolong.commons.response.ResponseResult;
import com.yaolong.commons.utils.MapperUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import com.yaolong.commons.base2.BaseController;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 居民管理 前端控制器
 * </p>
 *
 * @author yaolonga
 * @version v1.0.0
 * @since 2023-02-05
 */
@RestController
@RequestMapping("/api/opms/fix-resident")
@Api(tags = "居民管理")
public class FixResidentController extends BaseController<FixResident, FixResidentService> {
    @Autowired
    private FixResidentService fixResidentService;

    @GetMapping("/exportExcel")
    @ApiOperation("导出编码")
    public void export(HttpServletResponse response, FixResident form) {
        //查询要导出的数据
        try {
            FixResident data = new FixResident();
            BeanUtils.copyProperties(form, data);
            IPage<?> page = fixResidentService.page(form.getCurrent(), form.getSize(), form);
            ExcelUtil2.exportExcelX(page.getRecords(), null, "sheet1", FixResident.class, "居民信息表.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/getFields")
    @ApiOperation("获取导入字段")
    public ResponseResult getFields() {
        List<DictVo> dictVos = new ArrayList<>();
        FixResident fixResident = new FixResident();
        Class<?> aClass = fixResident.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            boolean annotationPresent = field.isAnnotationPresent(Excel.class);
            if (annotationPresent) {
                DictVo dictVo = new DictVo();
                String name = field.getAnnotation(Excel.class).name();
                String value = field.getName();
                dictVo.setLabel(name);
                dictVo.setValue(value);
                dictVos.add(dictVo);
            }
        }
        return ResponseResult.success(dictVos);
    }

    /**
     * 导入
     */
    @ApiOperation("导入编码")
    @Transactional(rollbackFor = RuntimeException.class)
    @PostMapping("/importExcel")
    public ResponseResult importResident(@RequestBody List<Map<String, Object>> from) {
        //UserEntity curUserInfoByRedis = userService.getCurUserInfoByRedis(request);
        //查询存货编码
        List<FixResident> list = fixResidentService.list();
        Class<? extends FixResident> aClass = new FixResident().getClass();
        List<FixResident> inventoryList = new ArrayList<>();
        for (Map<String, Object> item : from) {
            //JSON.parseObject(item,FixResident.class)
            FixResident fixResident = JSON.parseObject(JSON.toJSONString(item), FixResident.class);
            fixResident.setIssuingDate(LocalDate.now());
            inventoryList.add(fixResident);
        }
        if (inventoryList.isEmpty()) {
            return ResponseResult.failure("导入数据不能为空！");
        }
        try {
            boolean b = fixResidentService.saveBatch(inventoryList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("导入数据出错，请联系管理员！");
        }
        return ResponseResult.success("导入成功！");
    }
}
